package com.tinklabs.demo.fragment;

import com.tinklabs.demo.mvp.presenter.BasePresenter;


/**
 * Created by Jennifer on 2018/4/14.
 */
public abstract class PresenterFragment<P extends BasePresenter, V> extends BaseFragment {
    private P presenter;

    @Override
    protected void initTitle() {}

    @Override
    protected void initData() {
        initPresenter();
    }

    /**
     * Must implement presenter instance
     */
    private void initPresenter() {
        presenter = createPresenter();
        if (presenter == null) {
            throw new NullPointerException("presenter，空指针异常...");
        }

        V view = createView();
        if (view == null) {
            throw new NullPointerException("view，空指针异常...");
        }

        presenter.attachView(view);
    }

    public P getPresenter() {
        return presenter;
    }

    protected abstract P createPresenter();

    protected abstract V createView();

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (presenter != null) {
            presenter.detachView();
        }
    }

    @Override
    protected void reset() {
    }
}
